package com.nyist.myticket.biz.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nyist.myticket.biz.entity.User;
import com.nyist.myticket.biz.mapper.TbUserMapper;
import com.nyist.myticket.biz.service.TbUserService;
import com.nyist.myticket.core.base.BaseService;
import com.nyist.myticket.core.enums.DataStatusEnum;
import com.nyist.myticket.core.utils.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangxiaogen
 * @since 2021-01-05
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbUserServiceImpl extends BaseService<TbUserMapper, User> implements TbUserService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public User saveUser(User user) {

        int count = tbUserMapper.insert(user);
        if (count > 0) {
            return user;
        }
        return new User();
    }

    @Override
    public boolean deleteToStatus(User user) {
        user.setStatus(DataStatusEnum.REMOVE.getCode());
        int count = tbUserMapper.updateById(user);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public User updateUser(User user) {
        return null;
    }

    @Override
    public User queryUserById(String id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("status",0);
        return tbUserMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> queryUserAll(String keywords) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<User> userList = tbUserMapper.selectList(queryWrapper);
        return userList;
    }

    @Override
    public List<User> listUserLikeName(String name) {
        return null;
    }

    @Override
    public List<User> listUser(User user) {
        Map map = BeanConvertUtil.beanCovertMap(user);
        return tbUserMapper.listUserInfo(map);
    }
}
